package io.flutter.plugins.camera;

import android.view.KeyEvent;

import java.util.List;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class KeyListenerActivity extends FlutterActivity {

    private MethodChannel channel;
    //是否拦截音量键
    private boolean interruptVolumeDown, interruptVolumeUp;

    private static final int KEYCODE_VOLUME_DOWN = 0;
    private static final int KEYCODE_VOLUME_UP = 1;

    public void init(BinaryMessenger messenger) {
        if(channel != null) return;
        channel = new MethodChannel(messenger, "plugins.flutter.io/key_listener");
        channel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if("initialize".equals(call.method)) {
                    Object obj = call.arguments;
                    if(obj instanceof List) {
                        List<Integer> keys = (List<Integer>) obj;
                        for(Integer integer : keys) {
                            if(integer == KEYCODE_VOLUME_DOWN) interruptVolumeDown = true;
                            if(integer == KEYCODE_VOLUME_UP) interruptVolumeUp = true;
                        }
                    }
                } else if("dispose".equals(call.method)) {
                    interruptVolumeDown = false;
                    interruptVolumeUp = false;
                }
            }
        });
    }

    public void release() {
        if(channel != null) channel.setMethodCallHandler(null);
        channel = null;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (channel != null) {
            if (interruptVolumeDown && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                channel.invokeMethod("onKeyDown", KEYCODE_VOLUME_DOWN);
                return true;
            } else if (interruptVolumeUp && keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                channel.invokeMethod("onKeyDown", KEYCODE_VOLUME_UP);
                return true;
            }
        }
        if(keyCode == KeyEvent.KEYCODE_POWER) {
            CameraUtils.Log("user click power button", null);
        } else if(keyCode == KeyEvent.KEYCODE_HOME) {
            CameraUtils.Log("user click home button", null);
        } else if(keyCode == KeyEvent.KEYCODE_MENU) {
            CameraUtils.Log("user click menu button", null);
        } else if(keyCode == KeyEvent.KEYCODE_BACK) {
            CameraUtils.Log("user click back button", null);
        } else {
            CameraUtils.Log("user click button:" + keyCode, null);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (channel != null) {
            if (interruptVolumeDown && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                channel.invokeMethod("onKeyUp", KEYCODE_VOLUME_DOWN);
                return true;
            } else if (interruptVolumeUp && keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                channel.invokeMethod("onKeyUp", KEYCODE_VOLUME_UP);
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        release();
    }
}
